package ca.concordia.soen6461.wichf.navigation.action;

import java.util.Calendar;
import java.util.Date;

import ca.concordia.soen6461.wichf.controller.Action;
import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent.Type;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.navigation.event.ManageAppointmentEvent;
import ca.concordia.soen6461.wichf.navigation.view.HomeView;
import ca.concordia.soen6461.wichf.service.SchedulingService;

public abstract class DoManageAppointment implements Action {

	private SchedulingService schedulingService;
	
	public DoManageAppointment(SchedulingService schedulingService) {
		this.schedulingService = schedulingService;
	}
	
	@Override
	public void perform(Event event) {
		User user = event.getSession().get(User.class);
		ManageAppointmentEvent makeEvent = (ManageAppointmentEvent) event;
	    Calendar date = HomeView.getDate(makeEvent.getDay(), makeEvent.getTime());
		Date from = date.getTime();
		manageAppointment(schedulingService, makeEvent, user, from);
	}

	protected void manageAppointment(SchedulingService schedulingService, ManageAppointmentEvent event, User user, Date from) {
		int duration = HomeView.duration(getEventType());
		ScheduledEvent e = schedulingService.makeAppointment(user, from, duration, getEventType());
		if (e == null) {
			event.getMessages().add(
				"Could not make the specified appointment. There may be no more available rooms/doctors!"
			);
		}
	}

	protected abstract Type getEventType();

}
